﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：购物车管理
  //创建人：赵国伟
  //生成时间：2012/04/18 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class ShoppingCartsManage
    {

        private readonly DALShoppingCarts dalCart = new DALShoppingCarts();
        private const string _shoppingCartCookieTag = "ShoppingCartCookieTag";

        private const int _shoppingCartCookieExpire = 24 * 7;

        #region "添加指定的商品到购物车中； by 赵国伟 2012-04-18"
        /// <summary>
        /// 添加指定的商品到购物车中；
        /// </summary>
        /// <param name="cookiesId">Cookies编号</param>
        /// <param name="GoodsId"></param>
        /// <param name="Quantity"></param>
        /// <returns></returns>
        public ReturnValue Save(ShoppingCarts c)
        {
            ReturnValue ret = new ReturnValue();
            if (string.IsNullOrEmpty(c.CookiesID))
            {
                ret.Message = "CookieID不能为空！";
                return ret;
            }
            if (c.GoodsID <= 0)
            {
                ret.Message = "商品编号不能为空！";
                return ret;
            }
            if (c.PackUnitID <= 0)
            {
                ret.Message = "包装单位不能为空！";
                return ret;
            }

            var list = dalCart.QueryList(1, " cookiesID='" + c.CookiesID + "' and GoodsId=" + c.GoodsID + " and PackUnitID=" + c.PackUnitID, "");
            if (list == null)
            {
                if (dalCart.Add(c) > 0)
                {
                    ret.Message = "添加成功！";
                    ret.Result = ResultType.Success;
                }
                
            }
            else
            {
                var s = list[0];
                s.Quantity += c.Quantity;
                if (dalCart.Update(s) > 0)
                {
                    ret.Message = "添加成功！";
                    ret.Result = ResultType.Success;
                }
            }
            return ret;
        }
        #endregion

        #region "从购物车中删除指定的商品； by 赵国伟 2012-04-18"
        /// <summary>
        /// 从购物车中删除指定的商品；
        /// </summary>
        /// <param name="cookiesId"></param>
        /// <param name="GoodsID"></param>
        /// <param name="PackUnitID">包装单位</param>
        /// <returns></returns>
        public ReturnValue Delete(string cookiesId, int GoodsID,int PackUnitID)
        {
            ReturnValue ret = new ReturnValue();
            if (dalCart.Delete(cookiesId, GoodsID,PackUnitID) > 0)
            {
                ret.Message = "删除成功！";
                ret.Result = ResultType.Success;
            }
            return ret;
        }

        public ReturnValue Delete(string cookiesId, int shoppingCartID)
        {
            ReturnValue ret = new ReturnValue();
            if (dalCart.Delete(cookiesId, shoppingCartID) > 0)
            {
                ret.Message = "删除成功！";
                ret.Result = ResultType.Success;
            }
            return ret;
        }
        #endregion

        #region "清空指定用户的购物车信息； by 赵国伟 2012-04-18"
        /// <summary>
        /// 清空指定用户的购物车信息；
        /// </summary>
        /// <param name="cookiesId"></param>
        /// <returns></returns>
        public ReturnValue Clear(string cookiesId)
        {
            ReturnValue ret = new ReturnValue();
            try
            {
                dalCart.Clear(cookiesId);
                ret.Message = "操作已成功！";
                ret.Result = ResultType.Success;
            }
            catch
            {

            }
            return ret;
        }
        #endregion

        #region "查询指定用户的购物车信息； by 赵国伟 2012-04-18"
        /// <summary>
        /// 查询指定用户的购物车信息；
        /// </summary>
        /// <param name="cookiesId"></param>
        /// <returns></returns>
        public List<ShoppingCartInfo> QueryList(string cookiesId)
        {
            string cond = "CookiesID='" + cookiesId + "' ";
            string orderby = "ShoppingCartID desc";
            List<ShoppingCarts> source = dalCart.QueryList(cond, orderby);
            List<ShoppingCartInfo> target = null;
            if (source != null)
            {
                target = new List<ShoppingCartInfo>();
                DALGoodsPictures dalPicture = new DALGoodsPictures();
                Dictionary<int, string> dictGoodsName = BLLHelper.GenerateToHash<Goods>("GoodsID in(Select GoodsID From ShoppingCarts Where " + cond + ")", GoodsEnum.GoodsName);
                Dictionary<int, string> dictGoodsPic = dalPicture.QueryMajorPictureToHash("GoodsID in(Select GoodsID From ShoppingCarts Where " + cond + ")", GoodsPicturesEnum.SuperSmallPictureUrl);
                foreach (ShoppingCarts s in source)
                {
                    ShoppingCartInfo t = new ShoppingCartInfo();
                    t.CartInfo = s;
                    if (dictGoodsName.ContainsKey(s.GoodsID))
                        t.GoodsName = dictGoodsName[s.GoodsID];
                    if (dictGoodsPic.ContainsKey(s.GoodsID))
                        t.SuperSmallPicture = dictGoodsPic[s.GoodsID];
                    target.Add(t);
                }
            }
            return target;
        }
        #endregion


        #region "本机购物车维护操作； by 党明 2012-04-27"
        /// <summary>
        /// 设置本机当前用户购物车Cookie
        /// </summary>
        private void AddShoppingCartCookieInfo(string tag)
        {
            if (string.IsNullOrEmpty(Utility.GetCookies(tag)))
            {
                Utility.SetCookies(tag, Guid.NewGuid().ToString(), "", _shoppingCartCookieExpire);
            }
        }
        /// <summary>
        /// 获取本机用户cookie名称
        /// </summary>
        /// <returns>本机cookie名称</returns>
        public string GetShoppingCartCookieName(int userId)
        {
            var cookieName = userId.ToString();
            cookieName = Utility.GetCookies(_shoppingCartCookieTag);
            if (string.IsNullOrEmpty(cookieName))
            {
                AddShoppingCartCookieInfo(_shoppingCartCookieTag);
                return GetShoppingCartCookieName(userId);
            }

            return cookieName;
        }
        #endregion

        #region "调整购物车指定商品的数量； by 赵国伟 2012-05-14"
        /// <summary>
        /// 调整购物车指定商品的数量；
        /// </summary>
        /// <param name="goodsId"></param>
        /// <returns></returns>
        public ReturnValue ModifyQuantity(int userId, int shoppingCartID, int Quantity)
        {
            ReturnValue ret = new ReturnValue();
            if (dalCart.ModifyQuantity(GetShoppingCartCookieName(userId), shoppingCartID,  Quantity) > 0)
            {
                ret.Result = ResultType.Success;
                ret.Message = "更改成功！";
            }
            return ret;
        }
        #endregion


        #region "查询指定购物车中的商品数量； by 赵国伟 2012-08-10"
        /// <summary>
        /// 查询指定购物车中的商品数量；
        /// </summary>
       
        /// <returns></returns>
        public void QueryCartGoodsCounts(out int totalQuantity, out decimal totalMoney)
        {
             dalCart.QueryCartGoodsCounts(GetShoppingCartCookieName(0),out totalQuantity,out totalMoney);
        }
        #endregion


    }
}
